<template>
  <el-dialog
    class="dialog-body"
    :title="title"
    :visible="dialogVisible"
    width="60%"
    :before-close="doClose"
  >
    <div class="local-quill-editor">
      <quill-editor
        ref="myLQuillEditor"
        v-model="updateContent"
        :options="editorOption"
        class="editor"
      >
        <!-- @blur="onEditorBlur"
        @focus="onEditorFocus"
        @change="onEditorChange" -->
        >
      </quill-editor>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="doClose">取 消</el-button>
      <el-button type="primary" @click="doAction">{{ action }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 引入样式和quillEditor
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { updateHotelHtml } from '@/api/theme-hotel'

// 工具栏配置项
const toolbarOptions = [
  // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ['bold', 'italic', 'underline', 'strike'],
  // 引用  代码块-----['blockquote', 'code-block']
  ['blockquote', 'code-block'],
  // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
  [{ header: 1 }, { header: 2 }],
  // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{ list: 'ordered' }, { list: 'bullet' }],
  // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  [{ script: 'sub' }, { script: 'super' }],
  // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
  [{ indent: '-1' }, { indent: '+1' }],
  // 文本方向-----[{'direction': 'rtl'}]
  [{ direction: 'rtl' }],
  // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{ size: ['small', false, 'large', 'huge'] }],
  // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
  [{ header: [1, 2, 3, 4, 5, 6, false] }],
  // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
  [{ color: [] }, { background: [] }],
  // 字体种类-----[{ font: [] }]
  [{ font: [] }],
  // 对齐方式-----[{ align: [] }]
  [{ align: [] }],
  // 清除文本格式-----['clean']
  ['clean'],
  // 链接、图片、视频-----['link', 'image', 'video']
  ['image', 'video']
]

export default {
  name: 'LocalQuillEditor',
  // 注册 quillEditor
  components: {
    quillEditor
  },
  props: {
    dialogVisible: {
      type: Boolean,
      required: true,
      default: false
    },
    id: {
      type: Number,
      required: true,
      default: 0
    },
    content: {
      type: String,
      required: true,
      default: ''
    },
    code: {
      type: Number,
      required: true,
      default: 0
    },
    title: {
      type: String,
      required: true,
      default: ''
    },
    action: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      updateContent: '',
      editorOption: {
        modules: {
          toolbar: toolbarOptions
        },
        theme: 'snow',
        placeholder: '请输入正文'
        // Some Quill optiosn...
      }
    }
  },
  watch: {
    content() {
      this.updateContent = this.content
    }
  },
  methods: {
    // // 失去焦点事件
    // onEditorBlur(e) {
    //   console.log('onEditorBlur: ', e)
    // },
    // // 获得焦点事件
    // onEditorFocus(e) {
    //   console.log('onEditorFocus: ', e)
    // },
    // // 内容改变事件
    // onEditorChange(e) {
    //   console.log('onEditorChange: ', e)
    // },
    doAction() {
      updateHotelHtml(this.id, {
        code: this.code,
        html: this.updateContent
      }).then(res => {
        this.$message.success(res.message)
        this.actioned(res.data)
      })
    },
    actioned(res) {
      this.$emit('actioned', res)
      this.doClose()
    },
    doClose() {
      this.$emit('close')
    }
  }
}
</script>

  <style scoped lang="scss">
  .dialog{
    &-footer{
        margin-top: 16px;
    }
  }
  .local-quill-editor{
    margin-bottom: 32px;
  }
  .editor {
    height: 500px;
  }
  </style>
